#pragma once

#include <iostream>
#include <stdarg.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define Info 0
#define Debug 1
#define Warning 2
#define Error 3
#define Fatal 4

#define Screen 1
#define Onefile 2
#define Classfile 3
#define Logfile "log.txt"
class Log
{
public:
    Log()
    {
        _printmethod = Screen;
        _path = "./log/";
    }
    void Enable(int method)
    {
        _printmethod = method;
    }
    std::string LeveltoString(int level)
    {
        switch (level)
        {
        case Info:
            return "Info";
        case Debug:
            return "Debug";
        case Warning:
            return "Warning";
        case Error:
            return "Error";
        case Fatal:
            return "Fatal";
        default:
            return "None";
        }
    }

    // void Logmessage(int level, const char *format, ...)
    // {
    //     time_t t = time(nullptr);
    //     struct tm *ctime = localtime(&t);
    //     char initbuffer[1024];
    //     snprintf(initbuffer, sizeof(initbuffer), "[%s][%d-%d-%d %d:%d:%d]", LeveltoString(level).c_str(),
    //              ctime->tm_year + 1900, ctime->tm_mon + 1, ctime->tm_mday, ctime->tm_hour, ctime->tm_min, ctime->tm_sec);
    //     va_list s;
    //     va_start(s, format);
    //     char logbuffer[1024];
    //     vsnprintf(logbuffer, sizeof(logbuffer), format, s);
    //     va_end(s);
    //     char logtxt[2048];
    //     snprintf(logtxt, sizeof(logtxt), "%s %s\n", initbuffer, logbuffer);
    //     //printf("%s", logtxt);
    //     Printlog(level,logtxt);
    // }
    void Printlog(int level, const std::string &logtxt)
    {
        switch(_printmethod)
        {
            case Screen:
            {
                std::cout << logtxt;
                break;
            }
            case Onefile:
            {
                PrintOnefile(Logfile, logtxt);
                break;
            }
            case Classfile:
            {
                PrintClassfile(level, logtxt);
                break;
            }
            default:
            break;
        }
    }
    void PrintOnefile(const std::string &logname, const std::string &logtxt)
    {
        std::string _logname = _path + logname;
        int fd = open(_logname.c_str(), O_WRONLY | O_CREAT | O_APPEND, 0666);
        if(fd < 0)
        {
            return;
        }
        write(fd, logtxt.c_str(), logtxt.size());
        close(fd);
    }
    void PrintClassfile(int level, const std::string &logtxt)
    {
        std::string filename = Logfile;
        filename += ".";
        filename += LeveltoString(level);
        PrintOnefile(filename, logtxt);
    }
    ~Log()
    {}
    void operator()(int level, const char *format, ...)
    {
        time_t t = time(nullptr);
        struct tm *ctime = localtime(&t);
        char initbuffer[1024];
        snprintf(initbuffer, sizeof(initbuffer), "[%s][%d-%d-%d %d:%d:%d]", LeveltoString(level).c_str(),
                 ctime->tm_year + 1900, ctime->tm_mon + 1, ctime->tm_mday, ctime->tm_hour, ctime->tm_min, ctime->tm_sec);
        va_list s;
        va_start(s, format);
        char logbuffer[1024];
        vsnprintf(logbuffer, sizeof(logbuffer), format, s);
        va_end(s);
        char logtxt[2048];
        snprintf(logtxt, sizeof(logtxt), "%s %s\n", initbuffer, logbuffer);
        Printlog(level,logtxt);
    }
private:
    int _printmethod;
    std::string _path;
};


Log lg;